import 'reflect-metadata';
import '../../forenv'
import fetch from 'node-fetch'
import { BrowserWindow, app, ipcMain, session } from 'electron';
import { createEinf } from './support';
import { AllControllers } from './application/index';
import { initData } from './model/initData'
import gloabs from './common/Globals'
import model from '@backend/model'
import GlobalBrowserViewManager from '@backend/utils/globalBrowserViewManager'
import { getProxySettings, getAndTestProxySettings } from "get-proxy-settings";

globalThis.fetch = fetch;

process.on('unhandledRejection', err => {
    console.log('全局处理catch', err);
})
// This allows TypeScript to pick up the magic constants that's auto-generated by Forge's Webpack
// plugin that tells the Electron app where to look for the Webpack-bundled app code (depending on
// whether you're running in development or production).
declare const MAIN_WINDOW_WEBPACK_ENTRY: string;
declare const MAIN_WINDOW_PRELOAD_WEBPACK_ENTRY: string;
const userHome = process.env.HOME || process.env.USERPROFILE
// Handle creating/removing shortcuts on Windows when installing/uninstalling.
if (require('electron-squirrel-startup')) {
  app.quit();
}
let mainWindow: BrowserWindow = null;
console.log("process.env", process.env)
async function bootstrap() {
    initData()

    mainWindow = GlobalBrowserViewManager.createBrowserWindow()  
    
    mainWindow.maximize();
    mainWindow.show(); 
    mainWindow.setMovable(true)

    mainWindow.on('resize', (e : Event, d: any) => {
        mainWindow.webContents.send('window-max', mainWindow.isMaximized())
    })

    // and load the index.html of the app.
    mainWindow.loadURL(MAIN_WINDOW_WEBPACK_ENTRY);

    // Open the DevTools.
    mainWindow.webContents.openDevTools();

    await createEinf({
        // window to create
        window: mainWindow,
        // controllers will be automatically initialized
        controllers: AllControllers(),
        // custom items to inject
        injects: [{
            name: 'IS_DEV',
            inject: !app.isPackaged,
        }],
    })
}
// app.on('ready', bootstrap)

// Quit when all windows are closed, except on macOS. There, it's common
// for applications and their menu bar to stay active until the user quits
// explicitly with Cmd + Q.
app.on('window-all-closed', () => {
    if (process.platform !== 'darwin') {
      app.quit();
    }
  });
  
app.on('activate', () => {
// On OS X it's common to re-create a window in the app when the
// dock icon is clicked and there are no other windows open.
if (BrowserWindow.getAllWindows().length === 0) {
    bootstrap();}
});


ipcMain.on('window-max', () => {
    if(!mainWindow) return;
    if(mainWindow.isMaximized()) {
        mainWindow.restore()
    }else{
        mainWindow.maximize()
    }
})

ipcMain.on('window-min', () => {
    if(!mainWindow) return;
    mainWindow.minimize();
})

ipcMain.on('window-close', () => {
    if(!mainWindow) return;
    mainWindow.close();
})

app.on('ready', bootstrap);